package com.yulongtian.offers.day202209.day20220926;

/**
 * https://leetcode.cn/problems/fan-zhuan-dan-ci-shun-xu-lcof/?favorite=xb9nqhhg
 * @author YulongTian
 * @date 2022/9/26 - 10:36
 */
public class Test01 {
    public static void main(String[] args) {
        String s="a good   example";
        String s1 = reverseWords(s);
        System.out.println(s1);
    }

    public static String reverseWords(String s) {
        while (s.startsWith(" ")){
            s=s.substring(1);
        }
        while (s.endsWith(" ")){
            s=s.substring(0,s.length()-1);
        }
        StringBuilder res= new StringBuilder();
        String[] s1 = s.split(" ");
        for(int i=s1.length-1;i>=0;i--){
            if(s1[i].equals("")){
                continue;
            }
            res.append(s1[i]);
            if(i!=0){
                res.append(" ");
            }
        }

        return res.toString();
    }
}
